package com.vessel.decoration.mvp.ui.activity.me.manage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.addresspicker.huichao.addresspickerlibrary.CityPickerDialog;
import com.addresspicker.huichao.addresspickerlibrary.InitAreaTask;
import com.addresspicker.huichao.addresspickerlibrary.address.Province;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.vessel.decoration.BuildConfig;
import com.vessel.decoration.R;
import com.vessel.decoration.app.base.MySupportActivity;
import com.vessel.decoration.app.data.entity.ShopInfoResponse;
import com.vessel.decoration.di.component.mine.manage.DaggerBusinessInfoComponent;
import com.vessel.decoration.di.module.mine.manage.BusinessInfoModule;
import com.vessel.decoration.mvp.contract.mine.manage.BusinessInfoContract;
import com.vessel.decoration.mvp.presenter.mine.manage.BusinessInfoPresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author vesselzhang
 * @date 2017/12/8
 */
@Route(path = "/app/business/info")
public class MeBusinessInfoActivity extends MySupportActivity<BusinessInfoPresenter> implements BusinessInfoContract.View {

    @BindView(R.id.tv_title)
    TextView mTitleTV;
    @BindView(R.id.businessinfo_name)
    EditText mNameEV;
    @BindView(R.id.businessinfo_address)
    EditText mAddressEV;
    @BindView(R.id.businessinfo_phone)
    EditText mPhoneEV;
    @BindView(R.id.businessinfo_type)
    TextView mTypeTV;
    @BindView(R.id.businessinfo_city)
    TextView mCityTV;
    @BindView(R.id.businessinfo_image)
    ImageView mImageIV;
    @BindView(R.id.businessinfo_image_add)
    ImageView mImageAddIV;
    @BindView(R.id.businessinfo_submit)
    Button mSubmitBt;

    private ShopInfoResponse shopInfo;
    private int typeId = -1;
    private ArrayList<Province> provinces = new ArrayList<>();
    private AppComponent mAppComponent;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerBusinessInfoComponent
                .builder()
                .appComponent(appComponent)
                .businessInfoModule(new BusinessInfoModule(this))
                .build()
                .inject(this);
        mAppComponent = appComponent;
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_me_business_info;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitleTV.setText("信息管理");
        shopInfo = (ShopInfoResponse) getIntent().getSerializableExtra("data");
        if (shopInfo == null) {
            showMessage("传参错误");
            killMyself();
            return;
        }
        mNameEV.setText(shopInfo.getShopName());
        mAddressEV.setText(shopInfo.getAddress());
        mPhoneEV.setText(shopInfo.getPhone());
        mCityTV.setText(shopInfo.getCity());
        if (!TextUtils.isEmpty(shopInfo.getShopPic())) {
            showPic(shopInfo.getShopPic());
        }

        mPresenter.queryTypeList(shopInfo.getTypeId());

        InitAreaTask initAreaTask = new InitAreaTask(activity, provinces);
        initAreaTask.execute(0);
        initAreaTask.setOnLoadingAddressListener(new InitAreaTask.onLoadingAddressListener() {
            @Override
            public void onLoadFinished() {

            }

            @Override
            public void onLoading() {

            }
        });
    }

    @OnClick({R.id.businessinfo_image_add, R.id.businessinfo_submit, R.id.iv_left
            , R.id.businessinfo_type, R.id.businessinfo_city})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.businessinfo_image_add:
                mPresenter.addImage();
                break;
            case R.id.businessinfo_submit:
                mPresenter.submit(typeId, mNameEV.getText().toString()
                        , mAddressEV.getText().toString(), mPhoneEV.getText().toString()
                        , mCityTV.getText().toString());
                break;
            case R.id.iv_left:
                killMyself();
                break;
            case R.id.businessinfo_type:
                mPresenter.showTypePicker();
                break;
            case R.id.businessinfo_city:
                new CityPickerDialog(activity, provinces, null, null, null
                        , (selectProvince, selectCity, selectCounty) -> {
                    StringBuilder address = new StringBuilder();
                    address.append(
                            selectProvince != null ? selectProvince
                                    .getAreaName() : "")
                            .append(selectCity != null ? selectCity
                                    .getAreaName() : "");
                    mCityTV.setText(address.toString());
                }).show();
                break;
        }
    }

    @Override
    public void showType(int typeId, String typeName) {
        mTypeTV.setText(typeName);
        this.typeId = typeId;
    }

    @Override
    public void showPic(String uri) {
        mImageIV.setVisibility(View.VISIBLE);
        mPresenter.setUri(uri);
        mAppComponent.imageLoader()
                .loadImage(activity, ImageConfigImpl
                        .builder()
                        .imageView(mImageIV)
                        .url(BuildConfig.APP_DOMAIN + uri)
                        .build());
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {
        ArmsUtils.makeText(activity, message);
    }

    @Override
    public void launchActivity(Intent intent) {

    }

    @Override
    public void killMyself() {
        finish();
    }
}
